package com.hgy.controller;

import com.github.pagehelper.Page;
import com.hgy.common.JsonResult;
import com.hgy.entity.User;
import com.hgy.service.OrderService;
import com.hgy.utils.StrUtils;
import com.hgy.vo.VOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author : ang
 * @Program: carSystem
 * @Title : OrderController
 * @Date : 2020-08-28 10:50
 **/
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping("/add.do")
    @ResponseBody
    public JsonResult addOrder(HttpSession session, Integer cid, Integer getid, Integer backid, String oprice) {
        JsonResult jsonResult = null;
        User user = (User) session.getAttribute(StrUtils.LOGIN_USER);

        if (user == null) {
            jsonResult = new JsonResult(2, "未登录");
        } else {
            try {
                orderService.addOrder(user.getId(), cid, getid, backid, oprice);
                jsonResult = new JsonResult(1, "预定成功");
            } catch (Exception e) {
                jsonResult = new JsonResult(0, e.getMessage());
            }
        }
        return jsonResult;
    }

    @RequestMapping("/all.do")
    @ResponseBody
    public JsonResult selectAll(HttpSession session, Integer page) {
        User user = (User) session.getAttribute(StrUtils.LOGIN_USER);
        List<VOrder> list = orderService.selectAll(user.getId(), page);
        long total = ((Page) list).getTotal();
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", list);
        return new JsonResult(1, map);
    }

    @RequestMapping("/delete.do")
    @ResponseBody
    public JsonResult delete(Integer oid) {
        orderService.deleteOrder(oid);
        return new JsonResult(1, "删除成功");
    }
}
